package zsc.ruanc.practicaltraining.model.result;

import lombok.Getter;

/**
 * 状态码枚举
 */
@Getter
public enum ResultCode {

    // 数据操作错误定义
    SUCCESS(200, "响应成功!"),
    BODY_NOT_MATCH(400,"请求的数据格式不符!"),
    SIGNATURE_NOT_MATCH(401,"请求的数字签名不匹配!"),
    NOT_FOUND(404, "未找到该资源!"),
    INTERNAL_SERVER_ERROR(500, "服务器内部错误!"),
    SERVER_BUSY(503,"服务器正忙，请稍后再试!"),
    ERROR(-1, "错误的请求"),
    NOT_FOUND_CODE(-2, "没有找到与之对应的code"),
    REPEAT_EMAIL(-3, "邮箱已存在"),
    REPEAT_NAME(-4, "用户名已存在"),
    NOT_FOUND_USER(-5,"未找到用户"),
    ERROE_EMAILCODE(-6, "验证码错误"),
    NOT_FOUND_EMAILCODE(-7, "没有对应的验证码"),
    ERROR_NAME_OR_PASSWORD(-8, "用户名或密码错误"),
    INVALID_TOKEN(-9, "无效token"),
    DATA_OVERDUE(-10, "过期信息"),
    BLOCK(-11, "账号被封锁"),
    FILE_ERROR(-12, "文件传输出错"),
    AUTHORITY_ERROR(-13, "权限不足")
    ;

    private int code;

    private String msg;

    ResultCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 根据错误码找到对应的枚举,如果找不到，返回-2的枚举对象
     * @param code
     * @return
     */
    public static ResultCode findCode(int code) {
        for (ResultCode e : ResultCode.values()) {
            if (e.getCode() == code) {
                return e;
            }
        }
        return ResultCode.NOT_FOUND_CODE;
    }

}
